package siruna.WE.DeviceDependent;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.ZipFile;

import siruna.WE.DeviceDependent.FileManager.FileManager;
import siruna.WE.DeviceDependent.rendering.OverView;
import siruna.WE.DeviceDependent.rendering.WidgetView;
import siruna.WE.core.Engine;
import siruna.WE.core.model.FunctionalBlock;
import siruna.WE.core.model.VisualBlock;
import siruna.WE.core.model.container.PageImpl;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TextView;

public class WidgetEngine1 extends Activity implements DeviceDependentListener {
	FileManager fm;
	private Engine en;
	private int requestedWidgetId;
	private static LocationManager myManager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		fm = new FileManager(this);

		// WidgetId, ZipFile
		HashMap<Integer, ZipFile> zip = fm.getZipFiles();

	
		en = new Engine();

		en.setDeviceFunctBlockLocation(Global.DeviceDependentFunctionBlocks);
		en.setZipFiles(zip);
		en.setListener(this);

		// om de verschillende widgetviews te kunnen aanspreken
		BroadcastReceiverImpl receiver = new BroadcastReceiverImpl();
		((AndroidApplication) getApplication()).setReceiver(receiver);
		IntentFilter filter = new IntentFilter("UPDATE_VISUAL_BLOCK");
		registerReceiver(receiver, filter);

		HashMap<Integer, InputStream> icons = en.requestIcons();

		((AndroidApplication) getApplication()).setIcons(icons);

		Intent i = new Intent(getApplicationContext(), OverView.class);

		// Log.i(Global.TAG, "b4 pressed - about to launch sub-activity");
		// the results are called on widgetActivityCallback
		startActivityForResult(i, Global.OVERVIEW);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == Global.OVERVIEW) {

			System.out.println("requestCode:" + requestCode + "\n"
					+ "resultCode: " + resultCode + "\n" + "widgetId: "
					+ data.getIntExtra("widgetId", -1));

			LinearLayout layout = new LinearLayout(this);

			layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					android.view.ViewGroup.LayoutParams.FILL_PARENT));

			ProgressBar pb = new ProgressBar(this);
			TextView tv = new TextView(this);
			tv.setText("Widget is loading ...");

			layout.addView(pb);
			layout.addView(tv);

			int id = data.getIntExtra("widgetId", -1);
			System.out.println("id: " + id);
			if (id != -1) {
				requestedWidgetId = id;
				en.requestWidget(id);
			}
			setContentView(layout);
		}

		// Closing
		switch (resultCode) {
		case Global.SUBACTIVITY_RESULTCODE_CHAINCLOSE:
			this.setResult(Global.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
			this.finish();
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void updateWidget(int widgetId) {
		System.out.println("updateWidget, id: " + widgetId);
		System.out.println("updateWidget, requestedWidgetId: "
				+ requestedWidgetId);
		if (widgetId == requestedWidgetId) {

			Intent i = new Intent(getApplicationContext(), WidgetView.class);
			i.putExtra("widgetId", widgetId);

			((AndroidApplication) getApplication()).addWidget(widgetId, en
					.getWidget(widgetId));

			startActivityForResult(i, Global.WIDGETVIEW);

		}

	}

	@Override
	public void updateVisualBlock(int widgetId, String visBlockId) {
		// TODO Auto-generated method stub
		System.out.println("in de engine: widgetid: " + widgetId
				+ "visblockID: " + visBlockId);

		// doorgeven aan de view welke veranderd is
		Intent i = new Intent("UPDATE_VISUAL_BLOCK");
		i.putExtra("widgetId", widgetId);
		i.putExtra("widgetvisBlockId", visBlockId);
		// wordt naar alle active widgets gestuurd
		sendBroadcast(i);

	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);

		MenuItem item = menu.add("Close");
		// item.setIcon(R.drawable.paint);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);

		if (item.getTitle().equals("Close")) {
			this.setResult(Global.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
			this.finish();

		}
		return true;
	}

	@Override
	public HashMap<String, FunctionalBlock> getDevideFunctionalBlocks(
			HashMap<String, FunctionalBlock> hm) {
		
		HashMap<String, FunctionalBlock> ret = new HashMap<String, FunctionalBlock>();
		DeviceFunctionalBlockFactory dfbf = new DeviceFunctionalBlockFactory(this);

		for (String s : hm.keySet()) {

			FunctionalBlock dfb = dfbf.createDeviceFunctionalBlock(hm.get(s));
			ret.put(s, dfb);

		}
		return ret;
	}

}